package com.renbang.action.common;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;


public class LoginFilter implements Filter{
	FilterConfig config;
	Logger log = Logger.getLogger(Filter.class);
	public void destroy() {
	}
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain chain) throws IOException, ServletException {
		
		/*System.out.println("登录拦截");*/
		HttpServletRequest req=(HttpServletRequest) arg0;
		HttpServletResponse resp=(HttpServletResponse) arg1;
//		resp.setContentType("application/x-javascript");
		log.info("Login Filter.....");
		if(req.getSession().getAttribute("user")!=null||req.getRequestURI().contains("common")){
			chain.doFilter(req, resp);//放行
		}else{
			//未登录访问其它非common页面重定向到登录页面
			resp.sendRedirect(req.getContextPath()+"/common/login");
		}
		
	}
	public void init(FilterConfig arg0) throws ServletException {
		this.config = arg0;
	}

}
